package com.good.domain.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * 日期工具
 * @author lh02
 */
public class DateUtil {

    /**
     * 获取当天剩余时间单位:毫秒
     * @return
     */
    public static Long getTodaySurplusMill(){
        LocalDateTime toNight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        long between = ChronoUnit.MILLIS.between(LocalDateTime.now(), toNight);
        return between;
    }

    /**
     * 获取当天剩余时间单位:秒
     * @return
     */
    public static Long getTodaySurplusSecond(){
        LocalDateTime toNight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        long between = ChronoUnit.SECONDS.between(LocalDateTime.now(), toNight);
        return between;
    }

    /**
     * 获取本周剩余时间单位:秒
     * @return
     */
    public static Long getWeekSurplusSecond(){
        LocalDateTime weekEnd = LocalDateTime.now().with(DayOfWeek.SUNDAY).plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        long between = ChronoUnit.SECONDS.between(LocalDateTime.now(), weekEnd);
        return between;
    }
    /**
     * 字符串转换成日期
     * @param str
     * @return date
     */
    public static Date StrToDate(String str) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

}
